package BuddyTemplate;

/* Team Directories
 *
 * You are free to do whatever you want with this code. Frankly we don't give a damn.
 * 
 *  
 * Copyright GNU General Public License v3
 */

    /* The QuestionLayerCollection is a collection of QuestionLayers
     * In this current implementation, each "QuestionLayer" corrsponds to a difficulty
     * Level
     * 
     * The QuestionLayerCollection is defined, like QuestionLayer by two things which
     * wholly describe it's state
     * 
     * 1) The Collection of Questions themselves
     * 2) The Current Difficulty Level
     * 
     * 
     * @author Gabriel
     * 
     */

import java.util.*;
import java.io.*; 
import javax.swing.*;

public class QuestionLayerCollection implements QuestionInterface, Cloneable{
    

    
    // The QuestionLayer Collection - which contains layers of QuestionLayers
    ArrayList<QuestionLayer> Collection = new ArrayList<QuestionLayer>(); 
    AdaptiveEngine AE = new AdaptiveEngine((QuestionInterface)this);
    // The pointer
    int CurrentDifficultyLevel = 0;
        
    // For now Question Collection will only Consist of String Objects
    
    public QuestionLayerCollection(){
        // Empty Constructor
    }
    
    
    public boolean isFinal(){
     
        /* Case 1:
         * If we are at the final difficulty level AND we are at the last question of the 
         * final difficulty level, we are obviously at the last question. This makes the 
         * assumption that the last question (or the hardest question) is not answered.
         * 
         * This means once the user reaches the last question the quiz is over and there
         * is no chance he will "drop" a difficulty level
         */
        
        if (CurrentDifficultyLevel == Collection.size()-1 && Collection.get(CurrentDifficultyLevel).isFinal()){
            return true;
        }
        
        else {
            return false;
        }
        
    }
    
    public void addQuestion(Question NewQuestion){
        
        /* This method simply adds a quesiton to the Question Layer
         * it adds the question in the right layer, as determined by the 
         * questio difficulty. If there are "in-between" layers, say the 
         * last largest questionlayer was 2 and this is 7, it will "fill in"
         * the remaining layers
         */
        if ((NewQuestion.difficulty) < 0){
            
            NewQuestion.setDifficulty(0);
        }
        
        System.out.println("Difficulty: " + NewQuestion.difficulty);
        if(NewQuestion.difficulty >= Collection.size()){
            
            // If no such difficulty level exists, create a new difficulty level (Blank of course)
            for (int i = Collection.size(); i <= NewQuestion.difficulty; i++){
                Collection.add(i, new QuestionLayer());

            }
            
        }
        
        Collection.get(NewQuestion.difficulty).addQuestion(NewQuestion);     
        
    }
    
    
    public void setDifficulty(int x){

        CurrentDifficultyLevel = x;
        
    }
    
    
    
    public Question nextQuestion(){
        
        System.out.println("The Current Difficulty Level is:" + CurrentDifficultyLevel);
        AE.checkDifficulty(SaveGame.score);
        /* NextQuestion retrieves the next question in the collection of questions, 
         * "sorted" by difficulty. This method will work assuming
         * that it is not a final question (isFinal is false)
         */
        
        /*
         
        // First we save the game each time this method is called, as this means there is 
        // a change in "state" for some of the QuestionLayers and the Difficulty Level
        
        // We save the game difficulty
        SaveGame.difficulty = CurrentDifficultyLevel;
        
        // And we save the difficulty level for all the questions
        ArrayList<Integer> temp = new ArrayList<Integer>();
            
        for (Iterator i = Collection.iterator(); i.hasNext();){
            
            temp.add(((QuestionLayer)i.next()).QuestionAt);
            
        }
        
        SaveGame.QuestioAt = temp;
        
        */
        
        // We check if this is indeeed the last question
        
        if (!Collection.get(CurrentDifficultyLevel).isFinal()){

            // IF there are more questions for the current level of difficulty, 
            // Get the next quesiton (in the level's current level of difficulty)
            return Collection.get(CurrentDifficultyLevel).nextQuestion();

        }
        
        else {
            /* If the user is not at the Highest diffixculty level 
             * (Collection.size() - Number of Difficulty Levels)
             * CurrentDifficultyLevel = the current difficulty level
             * then we proceed to the next level of difficulty
             */
            
            if (CurrentDifficultyLevel < Collection.size()){
                
               // We first increase the difficulty level by one. This difficulty level has to exist
               // due to the previous condition being true
                
                CurrentDifficultyLevel ++;
                
                // Now we check to make sure we're not at the second to last difficulty level.
                // If we are, then the next segment of code is skipped. If not, then we check
                // if there are any "empty" levels in the next few levels
                
                if (CurrentDifficultyLevel < Collection.size()){
                    // If the question collection is empty OR if there are no more questions left to go
                    // Proceed to the next difficulty level. Keep doing so till the condition is false
                    // (that is to say, there are questions left to be answered)
              
                    while(Collection.get(CurrentDifficultyLevel).size() == 0 || Collection.get(CurrentDifficultyLevel).isFinal()){
                        CurrentDifficultyLevel ++;

                    }
                
                }
                
                return Collection.get(CurrentDifficultyLevel).nextQuestion();
                
            }
            
            
            return new Question<Object, Object>();
        
        }
        
    }
    
    public void setQuestionAtLayer(ArrayList<Integer> thelist){
        
        // This method sets the QuestionAt variable for each
        // of the "Layers" which exist. This is used to set the state of 
        // a particular question
        
        if (thelist.size() == CurrentDifficultyLevel){
            
            for (int i = 0; i < thelist.size(); i++){

                Collection.get(i).QuestionAt = thelist.get(i);

            }
        }
    
    }
    
    public int CheckAnswer(ArrayList<Boolean> Choice)
    {
        // Checks the answer of a particular question
        
        return Collection.get(CurrentDifficultyLevel).CheckAnswer(Choice);
        
    }
    
    public int getDifficulty()
    {
        return CurrentDifficultyLevel;
    }
}
